<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    protected $table = 'products';
    //设置主键值
    protected $primaryKey = 'id';
    public $timestamps = true;

    public function setPicsAttribute($pics)
    {
        if (is_array($pics)) {
            $this->attributes['pics'] = json_encode($pics);
        }
    }

    public function getPicsAttribute($pics)
    {
        return json_decode($pics, true);
    }

    public function product_sku()
    {
        return $this->hasMany(ProductSku::class, 'product_id', 'id');
    }

//    public function getKeyIdsAttribute($value)
//    {
//        $arr = json_decode($value, true);
//        $str = '';
//        if ($arr) {
//            $arrKeys = array_keys($arr);
//
//            $keys = ProductAttrKey::all()->toArray();
//            foreach ($keys as $v) {
//                if (in_array($v->id, $arrKeys)) {
//                    $str .= $v->attr_name;
//                }
//            }
//        }
//        return $str;
//    }

    public function setKeyIdsAttribute($value)
    {
        if (is_array($value)) {
            $this->attributes['key_ids'] = json_encode($value);
        }
    }

    public function setBaseParameterAttribute($value)
    {
        if (is_array($value)) {
            $this->attributes['base_parameter'] = json_encode(array_values($value));
        }
    }

    public function getBaseParameterAttribute($value)
    {
        return json_decode($value, true);
    }

    public function setGrapeVarietyAttribute($value)
    {
        if (is_array($value)) {
            $this->attributes['grape_variety'] = json_encode(array_values($value));
        }
    }

    public function getGrapeVarietyAttribute($value)
    {
        return json_decode($value, true);
    }

    public function setWineTasterAttribute($value)
    {
        if (is_array($value)) {
            $this->attributes['wine_taster'] = json_encode(array_values($value));
        }
    }

    public function getWineTasterAttribute($value)
    {
        return json_decode($value, true);
    }

    public function setPackageAttribute($value)
    {
        if (is_array($value)) {
            $this->attributes['package'] = json_encode(array_values($value));
        }
    }

    public function getPackageAttribute($value)
    {
        return json_decode($value, true);
    }
}
